local defaults = {
	dispelAnnounceChatFrame = nil;
	dispelAnnounceChannel = nil;
	dispelAnnounceSplash = true;
	dispelAnnounceSink = nil;
	showAbsorbs = true;
	showDispels = true;
	dispelAnnounceMessage = "%buff removed!";
};

local version = "0.1";

local desc = "DispelAnnounce "..version;

local AURADISPELOTHER3_PARSE = "(.+)'s (.+) is removed by (.+)'s (.+).";

local SPELLLOGABSORBOTHER_PARSE = "(.+)'s (.+) is removed by (.+)'s (.+).";

local SPELLLOGABSORBOTHEROTHER_PARSE = "(.+)'s (.+) is absorbed by (.+).";

local L = {
	["Grounding Totem"] = "Grounding Totem";
	["Purge"] = "Purge";
	["Devour Magic"] = "Devour Magic";
	["Dispel Magic"] = "Dispel Magic";
	["Mass Dispel"] = "Mass Dispel";
};

function SarfFix2:DispelAnnounce_Init()

	for k, v in pairs(defaults) do
		if ( SarfFix2.settings[k] == nil ) then SarfFix2.settings[k] = v; end
	end


	local class = nil;
	_, class = UnitClass("player");
	if ( class == "WARLOCK" ) or ( class == "PRIEST" ) or ( class == "SHAMAN" ) then
		ChatFrame1:AddMessage(desc.." loaded");
		SarfFix2:HookEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_DAMAGE", "DispelAnnounce_DamageSpell");
		SarfFix2:HookEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE", "DispelAnnounce_DamageSpell");
		SarfFix2:HookEvent("CHAT_MSG_SPELL_BREAK_AURA", "DispelAnnounce_BreakAuraEvent");
	end
end

function SarfFix2:IsShowAbsorbs()
	return SarfFix2.settings.showAbsorbs;
end

function SarfFix2:IsShowDispels()
	return SarfFix2.settings.showDispels;
end

function SarfFix2:SetShowAbsorb(newValue)
	if ( newValue ) and ( not SarfFix2:IsShowAbsorbs() ) then
		SarfFix2:HookEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_DAMAGE", "DispelAnnounce_DamageSpell");
		SarfFix2:HookEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE", "DispelAnnounce_DamageSpell");
	elseif ( not newValue ) and ( SarfFix2:IsShowAbsorbs() ) then
		SarfFix2:UnhookEvent("CHAT_MSG_SPELL_CREATURE_VS_SELF_DAMAGE", "DispelAnnounce_DamageSpell");
		SarfFix2:UnhookEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE", "DispelAnnounce_DamageSpell");
	end
	SarfFix2.settings.showAbsorbs = newValue;
end

function SarfFix2:SetShowDispel(newValue)
	if ( newValue ) and ( not SarfFix2:IsShowDispels() ) then
		SarfFix2:HookEvent("CHAT_MSG_SPELL_BREAK_AURA", "DispelAnnounce_BreakAuraEvent");
	elseif ( not newValue ) and ( SarfFix2:IsShowDispels() ) then
		SarfFix2:UnhookEvent("CHAT_MSG_SPELL_BREAK_AURA", "DispelAnnounce_BreakAuraEvent");
	end
	SarfFix2.settings.showDispels = newValue;
end

function SarfFix2:DispelAnnounce_ParseBreakAuraEvent(eventData)
	local v1, v2, v3, v4 = string.find(eventData, AURADISPELOTHER3_PARSE);
	return v1, v2, v3, v4;
end

function SarfFix2:DispelAnnounce_ParseAbsorbEvent(eventData)
	local v1, v2, v3 = string.find(eventData, SPELLLOGABSORBOTHEROTHER_PARSE);
	if ( v3 == nil ) then
		v2, v3 = string.find(eventData, SPELLLOGABSORBOTHER_PARSE);
		v1 = nil;
	end
	return v1, v2, v3;
end

function SarfFix2:DispelAnnounce_BreakAuraEvent(eventData)
	local affectedName, affectedBuff, sourceName, sourceSkill = SarfFix2:DispelAnnounce_ParseBreakAuraEvent(eventData);
	if ( sourceName == UnitName("player") ) or ( ( UnitExists("pet") ) and ( UnitName("pet") == sourceName ) ) then 
		SarfFix2:DispelAnnounce_Notify(affectedName, affectedBuff, sourceName, sourceSkill);
	end
end

function SarfFix2:DispelAnnounce_DamageSpell(eventData)
	local affectedName, affectedSpell, absorbSource = SarfFix2:DispelAnnounce_ParseAbsorbEvent(eventData);
	if ( absorbSource == L["Grounding Totem"] ) then
		SarfFix2:DispelAnnounce_ShowMessage(string.format("%s absorbed", affectedSpell));
	end
end

function SarfFix2:DispelAnnounce_Notify(affectedName, affectedBuff, sourceName, sourceSkill)
	local actualMessage = self.settings.dispelAnnounceMessage;
	if ( not actualMessage ) then
		ChatFrame1:AddMessage("DA: could not find actual message", 1, 0, 0);
		return;
	end
	actualMessage = string.gsub(actualMessage, "%%buff", affectedBuff);
	actualMessage = string.gsub(actualMessage, "%%skill", sourceSkill);
	actualMessage = string.gsub(actualMessage, "%%target", affectedName);
	actualMessage = string.gsub(actualMessage, "%%source", sourceName);
	SarfFix2:DispelAnnounce_ShowMessage(actualMessage);
end

function SarfFix2:DispelAnnounce_ShowMessage(message)
	if ( message == nil ) then
		SarfFix2:Debug("DispelAnnounce_ShowMessage received nil as parameter");
		return;
	end
	if ( self.settings.dispelAnnounceChatFrame ) then
		local v = self.settings.dispelAnnounceChatFrame;
		local frame = getglobal(v);
		if ( not frame ) then
			frame = getglobal("ChatFrame"..v);
		end
		if ( frame ) and ( type(frame.AddMessage) == "function" ) then
			frame:AddMessage(message);
		end
	end
	if ( self.settings.dispelAnnounceSplash ) then
		SarfFix2:ShowSplashMessage(message, 1, 1, 0);
	end
	if ( self.settings.dispelAnnounceSink ) then
		-- TODO: use SinkLib
	end
	if ( self.settings.dispelAnnounceChannel ) then
		-- TODO: SendChatMessage
	end
end


--SarfFix2:DispelAnnounce_Init();
